home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / Events.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  2KB  |  81 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from primitives import Delegate
  5. from logging import getLogger
  6. log = getLogger('events')
  7.  
  8. class BadEventException(Exception):
  9.     pass
  10.  
  11.  
  12. class EventMixin(object):
  13.     events = set()
  14.     
  15.     def __init__(self):
  16.         if not hasattr(self, 'handlers'):
  17.             self.handlers = { }
  18.             self.clear()
  19.         
  20.  
  21.     
  22.     def bind(self, ename, callback):
  23.         self.handlers[ename] += callback
  24.  
  25.     bind_event = bind
  26.     
  27.     def event(self, ename, *args):
  28.         if ename not in self.events:
  29.             raise BadEventException(type(self).__name__, ename, self.events)
  30.         
  31.         if self.handlers[ename]:
  32.             self.handlers[ename](*args)
  33.         
  34.  
  35.     
  36.     def unbind(self, ename, callback):
  37.         if self.handlers[ename]:
  38.             
  39.             try:
  40.                 self.handlers[ename] -= callback
  41.             except ValueError:
  42.                 pass
  43.             except:
  44.                 None<EXCEPTION MATCH>ValueError
  45.             
  46.  
  47.         None<EXCEPTION MATCH>ValueError
  48.  
  49.     
  50.     def clear(self):
  51.         for evtname in self.events:
  52.             self.handlers[evtname] = Delegate()
  53.         
  54.  
  55.     
  56.     def __getattr__(self, attr):
  57.         if attr in self.events:
  58.             return self.handlers[attr]
  59.         else:
  60.             return object.__getattribute__(self, attr)
  61.  
  62.  
  63.  
  64. def event(f):
  65.     import functools as functools
  66.     
  67.     def wrapper(self, *a):
  68.         name = f.func_name
  69.         retval = f(self, *a)
  70.         if isinstance(retval, tuple):
  71.             self.event(name, *retval)
  72.         elif retval not in (None, sentinel):
  73.             self.event(name, retval)
  74.         elif retval is None:
  75.             self.event(name, *a)
  76.         
  77.  
  78.     wrapper = (functools.wraps(f),)(wrapper)
  79.     return wrapper
  80.  
  81.